/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.BarrageStatusEnum;
import com.ykht.proj.coupon.entity.FxBarrage;
import com.ykht.proj.coupon.service.FxBarrageService;
import com.ykht.proj.coupon.vo.FxBarrageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 弹幕表
 *
 * @author proj code generator
 * @date 2021-07-07 16:20:00
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxbarrage" )
@Api(value = "fxbarrage", tags = "弹幕表管理")
@Slf4j
public class FxBarrageController {

    private final  FxBarrageService fxBarrageService;

    /**
     * 弹幕表 分页查询
     * @param page 分页对象
     * @param fxBarrageVo 弹幕表
     * @return
     */
    @ApiOperation(value = "弹幕表 分页查询", notes = "弹幕表 分页查询")
    @GetMapping("/page" )
    public R<IPage<FxBarrageVo>> getFxBarragePage(Page page, FxBarrageVo fxBarrageVo) {
		log.info("->弹幕表 分页查询:{}", JSONUtil.toJsonStr(fxBarrageVo));
		IPage<FxBarrageVo> iPage = fxBarrageService.getBarrageListPage(page, fxBarrageVo);
        return R.ok(iPage);
    }


    /**
     * 通过id查询弹幕表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('coupon_fxbarrage_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(fxBarrageService.getById(id));
    }

    /**
     * pc新增弹幕表并同时给视频添加弹幕数量
     * @param fxBarrage 弹幕表
     * @return R
     */
    @ApiOperation(value = "pc新增弹幕表并同时给视频添加弹幕数量", notes = "pc新增弹幕表并同时给视频添加弹幕数量")
    @SysLog("pc新增弹幕表并同时给视频添加弹幕数量" )
    @PostMapping("saveFxBarrage")
    public R saveFxBarrage(@RequestBody FxBarrage fxBarrage) {
		log.info("->pc新增弹幕表并同时给视频添加弹幕数量:{}", JSONUtil.toJsonStr(fxBarrage));
		fxBarrage.setType(1);
		fxBarrage.setStatus(BarrageStatusEnum.AT_WORK.getValue());
		fxBarrage.setCreateUser(SecurityUtils.getUserId());
		fxBarrage.setCreateTime(LocalDateTime.now());
		fxBarrage.setApprovalDate(LocalDateTime.now());
		fxBarrage.setPublishId(SecurityUtils.getUserId());
		fxBarrage.setPublishName(SecurityUtils.getUser().getUsername());
		fxBarrageService.saveFxBarrage(fxBarrage);
        return R.ok();
    }

	/**
	 * pc批量新增弹幕表并同时给视频添加弹幕数量
	 * @param fxBarrageVo 弹幕表集合
	 * @return R
	 */
	@ApiOperation(value = "pc批量新增弹幕表并同时给视频添加弹幕数量", notes = "pc批量新增弹幕表并同时给视频添加弹幕数量")
	@SysLog("pc批量新增弹幕表并同时给视频添加弹幕数量" )
	@PostMapping("saveBatchFxBarrage")
	public R saveBatchFxBarrage(@RequestBody FxBarrageVo fxBarrageVo) {
		log.info("->pc批量新增弹幕表并同时给视频添加弹幕数量:{}", JSONUtil.toJsonStr(fxBarrageVo));
		fxBarrageService.saveBatchFxBarrage(fxBarrageVo);
		return R.ok();
	}

	/**
	 * pc 审核弹幕
	 * @param fxBarrageVo 弹幕表
	 * @return R
	 */
	@ApiOperation(value = "pc 审核弹幕", notes = "pc 审核弹幕")
	@SysLog("pc 审核弹幕" )
	@PostMapping("updateFxBarrage")
	public R updateFxBarrage(@RequestBody FxBarrageVo fxBarrageVo) {
		log.info("->pc 审核弹幕:{}", JSONUtil.toJsonStr(fxBarrageVo));
		fxBarrageVo.setUpdateUser(SecurityUtils.getUserId());
		fxBarrageVo.setUpdateTime(LocalDateTime.now());
		fxBarrageVo.setApprovalDate(LocalDateTime.now());
		fxBarrageService.updateFxBarrage(fxBarrageVo);
		return R.ok();
	}

    /**
     * 修改弹幕表
     * @param fxBarrage 弹幕表
     * @return R
     */
    @ApiOperation(value = "修改弹幕表", notes = "修改弹幕表")
    @SysLog("修改弹幕表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('coupon_fxbarrage_edit')" )
    public R updateById(@RequestBody FxBarrage fxBarrage) {
        return R.ok(fxBarrageService.updateById(fxBarrage));
    }

    /**
     * 通过id删除弹幕表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除弹幕表", notes = "通过id删除弹幕表")
    @SysLog("通过id删除弹幕表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('coupon_fxbarrage_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(fxBarrageService.removeById(id));
    }

}
